import {
    wxfileTobase64
} from '../../../utils/util.js'
import {
    insuranceRecharge,
    addLongTermRecharge
} from '../../api/insurance.js'
import {
    getInsuranceProgramme,
    addRechargeRecord,
    getqueryreceiptaccountapi,
    ocrReadRechargeAmountApi,
    checkRechargeAmountApi
} from '../../../api/app'
import {
    shake,
} from '../../../utils/util'
import user from '../../../utils/user.js'
const app = getApp()
let base64 = ''
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isRepeat: false,
        ocrImg: '',
        ocrAmount: '',
        ocrNum: '请输入充值金额',
        preview: '',
        money: '',
        bankTitle: '',
        bankNum: '',
        bankName: '',
        insureCompanyType: '',
        rechargeType: '',
        yearList: ['1年', '2年', '3年'],
        yearIndex: '0',
        productType: '',
        bankLogo: '',
        stepList: [{
            text: '发起充值',
            hasNext: true
        },
        {
            text: '客服审核',
            hasNext: true
        },
        {
            text: '充值成功',
            hasNext: false
        },
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log('user', user)
        console.log(options, 'options1111111')
        this.setData({
            insureCompanyType: user.insureCompanyType,
            rechargeType: options.type
        })
        if (options.type == 'payRenew') {
            this.setData({
                bankLogo: '/packageInsurance/assets/img/insurance/safety-bank.png',
                bankTitle: '中国银行昆山城东支行',
                bankNum: '554 739 755 956',
                bankName: '苏州蓝豚互动信息科技有限公司'
            })
        } else if (options.type == 'InsureRechargePeriodZgrs') {
            // 长期保
            getqueryreceiptaccountapi({
                productType: 3
            }).then((res) => {
                console.log(res, 1674347868945789678945)
                this.setData({
                    bankTitle: res.openBank,
                    bankNum: res.bankNo,
                    bankName: res.bankName,
                    bankLogo: res.bankLogo,
                })
            })
        } else {
            getqueryreceiptaccountapi({
                productType: 2
            }).then((res) => {
                console.log(res, 'uhfshiugdugdihidohiodfikoio')
                this.setData({
                    bankTitle: res.openBank,
                    bankNum: res.bankNo,
                    bankName: res.bankName,
                    bankLogo: res.bankLogo,
                })
            })
        }

    },
    onClearIpt() {
        this.setData({
            money: ''
        })
    },
    onUnload() {
        console.log('卸载啦')
        base64 = ''
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        if (this.data.rechargeType === 'payRenew') {
            this.getInsuranceProgramme()
        }
    },
    moneyChange(event) {
        this.setData({
            money: event.detail.value
        })
        shake(() => {
            checkRechargeAmountApi({
                rechargeAmount: event.detail.value
            }).then((res) => {
                this.setData({
                    isRepeat: res
                })
            })
        }, 1000)
    },
    upload() {
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            success: (res) => {
                wxfileTobase64(res.tempFilePaths[0]).then(async (base) => {
                    let reason = await ocrReadRechargeAmountApi({
                        voucherUrl: base
                    })
                    console.log(reason, 'uhsfdhjs')
                    base64 = base
                    this.setData({
                        money: reason.ocrAmount,
                        ocrImg: reason.ocrUrl,
                        ocrAmount: reason.ocrAmount,
                        preview: res.tempFilePaths[0]
                    })
                    checkRechargeAmountApi({
                        rechargeAmount: reason.ocrAmount
                    }).then((res) => {
                        this.setData({
                            isRepeat: res
                        })
                    })
                })
            }
        })
    },
    copy(event) {
        wx.setClipboardData({
            data: event.currentTarget.dataset.copy, // 这个是要复制的数据
        })
    },
    submit() {
        if (!base64) {
            app.toast('请上传图片')
            return
        }
        if (!this.data.money && this.data.rechargeType !== 'payRenew') {
            app.toast('请填写充值金额')
            return
        }
        if (this.data.rechargeType === 'payRenew') {
            this.onAddRechargeRecord()
        } else if (this.data.rechargeType === 'InsureRechargePeriodZgrs') {
            this.addLongTermRecharge()
        } else {
            insuranceRecharge({
                rechargeAmount: this.data.money,
                voucherUrl: this.data.ocrImg,
                receiptId: user.receiptId,
                ocrAmount: this.data.ocrAmount,
                isRepeat: this.data.isRepeat? 1 : 0
            }).then(res => {
                console.log(res, '长期或者短期返回的内容')
                wx.redirectTo({
                    url: '/packageInsurance/pages/insurance-gtg-success/insurance-gtg-success?id=' + res,
                })
            })
        }
    },
    preview() {
        wx.previewImage({
            urls: [this.data.preview],
        })
    },
    // 切换年份
    onYearIndex(event) {
        const {
            index
        } = event.currentTarget.dataset
        this.setData({
            yearIndex: index
        })
    },
    // 根据参数获取内容
    async getInsuranceProgramme() {
        const result = await getInsuranceProgramme({
            // ele_sign_plan pay_account_plan
            dictType: 'pay_account_plan'
        })
        console.log(result, 'result')
        this.setData({
            yearList: result
        })
    },
    async onAddRechargeRecord() {
        const {
            dictLabel,
            dictValue
        } = this.data.yearList[this.data.yearIndex]
        addRechargeRecord({
            payMoney: dictValue,
            rechargeAmount: dictLabel,
            voucherUrl: base64,
            receiptId: user.receiptId
        }).then(res => {
            console.log(res, 'res续费返回的结果')
            wx.redirectTo({
                url: '/packageInsurance/pages/insurance-gtg-success/insurance-gtg-success?id=' + res + '&type=' + this.data.rechargeType,
            })
        })
    },
    // 长期保充值接口
    async addLongTermRecharge() {
        addLongTermRecharge({
            rechargeAmount: this.data.money,
            ocrAmount: this.data.ocrAmount,
            voucherUrl: this.data.ocrImg,
            receiptId: user.receiptId,
            isRepeat: this.data.isRepeat ? 1 : 0
        }).then(res => {
            console.log(res, 'res续费返回的结果')
            wx.redirectTo({
                url: '/packageInsurance/pages/insurance-gtg-success/insurance-gtg-success?id=' + res + '&type=' + this.data.rechargeType,
            })
        })
    }
})